package com.milo.springboot.eventListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Value("${server.port}")
    private String port;

    @Autowired
    Environment environment;

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @Autowired
    ApplicationContext applicationContext1;

    @RequestMapping("/login")
    public String login(@RequestParam("username") String username){
        applicationContext.publishEvent(new LoginEvent(username));
        //new LoginListener().onApplicationEvent(new LoginEvent(username));
        return "success";
    }

    @RequestMapping("/logout")
    public String logout(@RequestParam("username") String username){
        String port1 = environment.getProperty("local.server.port");
        applicationContext.publishEvent(new LogoutEvent(username));
        return "success";
    }

}
